#include "player_phonon.h"

#include <phonon/mediaobject.h>
#include <phonon/AudioOutput>

#include <QVBoxLayout>

#include <QDebug>

namespace plok {

    Player_phonon::Player_phonon() :
            m_mediaObject (NULL),
            m_video_widget (NULL),
            m_audio_out (new Phonon::AudioOutput( Phonon::VideoCategory )),
            m_audio_file (NULL),
            m_video_window ()
    {
        m_video_window.setMinimumSize(200,200);
    }

    Player_phonon::~Player_phonon ()
    {
        if (m_mediaObject)
            delete m_mediaObject;

        if (m_audio_out)
            delete m_audio_out;

        if (m_video_widget)
            delete m_video_widget;
    }

    void Player_phonon::open (const std::string& path)
    {
        if (m_mediaObject)
            delete m_mediaObject;
        if (m_video_widget)
            delete m_video_widget;


        m_mediaObject = new Phonon::MediaObject ();
        m_mediaObject->setCurrentSource( QString::fromStdString( path ) );

        if (m_mediaObject->hasVideo()) {
            qDebug() << "hadVideo()";
            QVBoxLayout *vLayout = new QVBoxLayout(&m_video_window);

            m_video_widget = new Phonon::VideoWidget (&m_video_window);
            vLayout->addWidget(m_video_widget);

            m_video_window.setLayout(vLayout);




            if (Phonon::createPath(m_mediaObject, m_video_widget).isValid()){
                m_video_window.show();
                m_video_widget->setAutoFillBackground(true);
            }
        }

        Phonon::Path phonon_path = Phonon::createPath( m_mediaObject, m_audio_out );


        connect (m_mediaObject, SIGNAL (stateChanged(Phonon::State, Phonon::State)), this, SLOT (on_player_stateChanged (Phonon::State, Phonon::State)));
        connect(m_mediaObject, SIGNAL(tick(qint64)), this, SLOT(on_player_timeChanged()));

        m_mediaObject->setTickInterval( 1000 );

        m_audio_file = new Audio_file_phonon (m_mediaObject->currentSource().fileName().toStdString());
    }

    void Player_phonon::enqueue (const std::string& path)
    {
        m_mediaObject->enqueue( QString::fromStdString(path) );
    }

    void Player_phonon::play ()
    {
        if (m_mediaObject->currentTime() == m_mediaObject->totalTime())
            m_mediaObject->seek(0);

        m_mediaObject->play();
    }

    void Player_phonon::stop ()
    {
        m_mediaObject->stop();
    }

    void Player_phonon::seek (qint64 time)
    {
        m_mediaObject->seek(time);
    }

    void Player_phonon::on_player_stateChanged ( Phonon::State newstate, Phonon::State oldstate )
    {
        std::string state_str;

        switch (newstate)
        {
            case Phonon::LoadingState: state_str = "loading"; break;
            case Phonon::StoppedState: state_str = "stopped"; break;
            case Phonon::PlayingState: state_str = "playing"; break;
            case Phonon::BufferingState: state_str = "buffering"; break;
            case Phonon::PausedState: state_str = "paused"; break;
            case Phonon::ErrorState:
                {
                    state_str = "error: ";
                    state_str += m_mediaObject->errorString().toStdString();
                } break;
                default: state_str = "unknown state";
        }

        qDebug() << "state changed: " << state_str.c_str();
    }

    void Player_phonon::on_player_timeChanged ()
    {
        emit updateTime (m_mediaObject->currentTime());
    }
}
